package com.djf.djfclickhouse.service.impl;

import com.djf.djfclickhouse.dao.OrderDao;
import com.djf.djfclickhouse.entity.Order;
import com.djf.djfclickhouse.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单服务实现类
 */
@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderDao orderDao;
    
    @Override
    public boolean createOrder(Order order) {
        if (order == null) {
            throw new IllegalArgumentException("订单对象不能为空");
        }
        
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        if (order.getCreateTime() == null) {
            order.setCreateTime(now);
        }
        if (order.getUpdateTime() == null) {
            order.setUpdateTime(now);
        }
        
        // 设置默认值
        if (order.getDeleted() == null) {
            order.setDeleted(0);
        }
        if (order.getOrderStatus() == null) {
            order.setOrderStatus(1); // 默认待支付
        }
        
        try {
            int result = orderDao.insert(order);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("创建订单失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public boolean batchCreateOrder(List<Order> orders) {
        if (CollectionUtils.isEmpty(orders)) {
            throw new IllegalArgumentException("订单列表不能为空");
        }
        
        LocalDateTime now = LocalDateTime.now();
        
        // 为每个订单设置默认值
        for (Order order : orders) {
            if (order.getCreateTime() == null) {
                order.setCreateTime(now);
            }
            if (order.getUpdateTime() == null) {
                order.setUpdateTime(now);
            }
            if (order.getDeleted() == null) {
                order.setDeleted(0);
            }
            if (order.getOrderStatus() == null) {
                order.setOrderStatus(1);
            }
        }
        
        try {
            int result = orderDao.batchInsert(orders);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("批量创建订单失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public Order getOrderById(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("订单ID不能为空或小于等于0");
        }
        
        try {
            return orderDao.selectById(id);
        } catch (Exception e) {
            throw new RuntimeException("查询订单失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public Order getOrderByOrderNo(String orderNo) {
        if (orderNo == null || orderNo.trim().isEmpty()) {
            throw new IllegalArgumentException("订单号不能为空");
        }
        
        try {
            return orderDao.selectByOrderNo(orderNo.trim());
        } catch (Exception e) {
            throw new RuntimeException("查询订单失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public List<Order> getOrdersByUserId(Long userId) {
        if (userId == null || userId <= 0) {
            throw new IllegalArgumentException("用户ID不能为空或小于等于0");
        }
        
        try {
            return orderDao.selectByUserId(userId);
        } catch (Exception e) {
            throw new RuntimeException("查询用户订单失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public List<Order> getOrdersWithPage(int pageNum, int pageSize) {
        if (pageNum <= 0) {
            throw new IllegalArgumentException("页码必须大于0");
        }
        if (pageSize <= 0 || pageSize > 1000) {
            throw new IllegalArgumentException("每页大小必须在1-1000之间");
        }
        
        int offset = (pageNum - 1) * pageSize;
        
        try {
            return orderDao.selectWithPage(offset, pageSize);
        } catch (Exception e) {
            throw new RuntimeException("分页查询订单失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public long countOrders(Long userId, Integer orderStatus) {
        try {
            return orderDao.countByCondition(userId, orderStatus);
        } catch (Exception e) {
            throw new RuntimeException("统计订单数量失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public boolean updateOrderStatus(Long id, Integer orderStatus) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("订单ID不能为空或小于等于0");
        }
        if (orderStatus == null || orderStatus < 1 || orderStatus > 5) {
            throw new IllegalArgumentException("订单状态必须在1-5之间");
        }
        
        try {
            int result = orderDao.updateOrderStatus(id, orderStatus);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("更新订单状态失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public boolean deleteOrder(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("订单ID不能为空或小于等于0");
        }
        
        try {
            int result = orderDao.deleteById(id);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("删除订单失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public boolean physicalDeleteOrder(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("订单ID不能为空或小于等于0");
        }
        
        try {
            int result = orderDao.physicalDeleteById(id);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("物理删除订单失败: " + e.getMessage(), e);
        }
    }
}